<template>

  <div class="topbar">
    <div class="wrap">
      <div class="left">欢迎来到叩丁狼积分商城</div>

      <ul class="right">
        <li>
          <img src="../assets/img/userImg.f8bbec5e.png" alt="">
          用户名：游客
        </li>
        <li>我的积分：--</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="loginbtn" @click="changeIsShowLoginModal(true)" v-if="!isLogined">登录</li>
        <li class="cart-btn" v-else>
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>9</strong>
        </li>
      </ul>
    </div>
  </div>

</template>

<script>
import { computed, watch } from 'vue'
import { mapMutations, mapState } from "vuex"
import { weiXinLoginAPI } from "@/request/api"
export default {
  created() {

    this.$router.onReady(async () => {
      let paramscode = this.$route.query.code
      if (paramscode) {
        let res = await weiXinLoginAPI({
          code: paramscode
        })
        console.log(res);


        if (res.code == 0) {
          alert("登录成功!")
          localStorage.setItem("x-auth-token", res["x-auth-token"])
          // 切换购物车
          this.changeIsLogined(true)
          // 清除地址栏中的code
          this.$router.push(this.$route.path)
        } else if (res.code == 400) {
          console.log('失效');
          alert("二维码以失效,请从新登录")
          // 重新登录
          this.changeIsShowLoginModal(true)
        } else if (res.code == 407) {
          // 弹出警示框
          alert("请使用手机号再登录一次！")
          // 弹出登录框
          this.changeIsShowLoginModal(true)
          // 本地存储uuid
          localStorage.setItem("uuid", res.uuid)
          // 清除地址栏中的code
          this.$router.push(this.$route.path)

        }
      } else {
        let token = localStorage.getItem("x-auth-token");
        this.changeIsLogined(Boolean(token));
      }
    })
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "logModal/changeIsShowLoginModal",
      changeIsLogined: "Logined/changeIsLogined",
    })
  },
  computed: {
    ...mapState({
      isLogined: state => state.Logined.isLogined
    })
  },
  watch: {
    "$route.path": {
      handler() {
        console.log("地址发生了该改变");
        let token = localStorage.getItem("x-auth-token")
        // if (token) {
        //   this.changeIsLogined(true)
        // } else {
        //   this.changeIsLogined(false)
        // }
        this.changeIsLogined(Boolean(token))
      }
    }
  }
}
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  line-height: 40px;
  background: #242B39;
  font-size: 14px;
  font-family: SourceHanSansSC;
  font-weight: 300;
  color: #FFFEFE;

  .wrap {
    display: flex;
    justify-content: space-between;

    .right {
      display: flex;

      li {
        margin: 0 10px;
        text-align: center;
        cursor: pointer;

        img {
          width: 26px;
          height: 26px;
          border-radius: 13px;
          vertical-align: middle;
        }

        &.loginbtn {
          width: 124px;
          height: 40px;
          background: #0A328E;


        }

        &.cart-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
          text-align: center;

          img {
            width: 20px;
            height: 19px;
            vertical-align: middle;
          }

          span {
            margin: 0 8px;
          }

          strong {
            width: 22px;
            height: 22px;
            line-height: 22px;
            display: inline-block;
            border-radius: 50%;
            background-color: #fd604d;
          }
        }
      }
    }
  }
}
</style>